package com.mykt.gateway.exception;

import com.alibaba.fastjson2.JSON;
import com.mykt.baseResult.RespondsResult;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * 网关异常处理
 */

public class MyWebException {

    public static Mono<Void>  exception(ServerWebExchange exchange,String msg){
        ServerHttpResponse response = exchange.getResponse();
        response.setStatusCode(HttpStatus.OK);
        response.getHeaders().add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
        RespondsResult respondsResult= RespondsResult.error(msg);
        DataBuffer dataBuffer = response.bufferFactory().wrap(JSON.toJSONString(respondsResult).getBytes());
        return response.writeWith(Mono.just(dataBuffer));

    }

}
